package com.example.cyclops;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MythConfig extends Activity {
	//ArrayList<String> tmpstr = new ArrayList<String>();

	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {  //获取 back键
			//confirm
			Global.iptable.flush();
			Toast.makeText(MythConfig.this, 
					"保存成功", Toast.LENGTH_SHORT).show();
			finish();
    	}
		return true;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.mythconfig);
		final Spinner spinner = (Spinner)findViewById(R.id.spinner1);
		final Button btnadd = (Button)findViewById(R.id.button1);
		final Button btnmodify = (Button)findViewById(R.id.button2);
		final Button btndel = (Button)findViewById(R.id.button3);

		final EditText mykeyname = (EditText)findViewById(R.id.editText1);
		final EditText myusername = (EditText)findViewById(R.id.EditText11);
		final EditText mypassword = (EditText)findViewById(R.id.EditText22);
		final EditText myip = (EditText)findViewById(R.id.EditText13);
		final EditText myport = (EditText)findViewById(R.id.EditText04);	
		//Global.tmpstr = new ArrayList<String>();
		//for(int i = 0;i < Global.iptable.size();i++){
		//	Global.tmpstr.add(Global.iptable.get(i).getKeyname());
		//}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_single_choice,
				Global.tmpstr);
		spinner.setAdapter(adapter);

		final Button btnback = (Button)findViewById(R.id.login_reback_btn);
		btnback.setOnClickListener(new OnClickListener(){	
				@Override
				public void onClick(View arg0) {

					Global.iptable.flush();
					Toast.makeText(MythConfig.this, 
							"保存成功", Toast.LENGTH_SHORT).show();
					finish();
				}
		}                                                                                                                                   );
		
        //添加事件Spinner事件监听    
        spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
        btnadd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//add to list
				
				MythIPtable myth = new MythIPtable(
						mykeyname.getText().toString(),
						myip.getText().toString(),
						myport.getText().toString(),
						myusername.getText().toString(),
						mypassword.getText().toString()
						);
				Global.iptable.writeMythIPtable(-1, myth);
				Global.tmpstr.add(mykeyname.getText().toString());
				Toast.makeText(MythConfig.this, "添加成功", Toast.LENGTH_SHORT).show();
				
			}
		});
        btnmodify.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//modify
				MythIPtable myth = new MythIPtable(
						mykeyname.getText().toString(),
						myip.getText().toString(),
						myport.getText().toString(),
						myusername.getText().toString(),
						mypassword.getText().toString()
						);
				Global.iptable.writeMythIPtable(spinner.getSelectedItemPosition(), myth);
				Global.tmpstr.set(spinner.getSelectedItemPosition(), mykeyname.getText().toString());
				Toast.makeText(MythConfig.this, "修改成功", Toast.LENGTH_SHORT).show();
			}
		});
        btndel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//delete
				Global.iptable.delMythIPtable(spinner.getSelectedItemPosition());
				Global.tmpstr.remove(spinner.getSelectedItemPosition());
				Toast.makeText(MythConfig.this, "删除成功", Toast.LENGTH_SHORT).show();
			}
		});
          
	}

    //使用数组形式操作  
    class SpinnerSelectedListener implements OnItemSelectedListener{  
		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			final EditText mykeyname = (EditText)findViewById(R.id.editText1);
			final EditText myusername = (EditText)findViewById(R.id.EditText11);
			final EditText mypassword = (EditText)findViewById(R.id.EditText22);
			final EditText myip = (EditText)findViewById(R.id.EditText13);
			final EditText myport = (EditText)findViewById(R.id.EditText04);
			// TODO Auto-generated method stub
			MythIPtable myth = Global.iptable.get(arg2);
			mykeyname.setText(myth.getKeyname());
			myusername.setText(myth.getUsername());
			mypassword.setText(myth.getPassword());
			myip.setText(myth.getIP());
			myport.setText(Integer.toString(myth.getPort()));
			
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			//final EditText mykeyname = (EditText)findViewById(R.id.editText1);
			//final EditText myusername = (EditText)findViewById(R.id.EditText01);
			//final EditText mypassword = (EditText)findViewById(R.id.EditText02);
			//final EditText myip = (EditText)findViewById(R.id.EditText03);
			//final EditText myport = (EditText)findViewById(R.id.EditText04);
			//mykeyname.setText("");
			//myusername.setText("");
			//mypassword.setText("");
			//myip.setText("");
			//myport.setText("");
		}  
    }
}
